package com.tchintech.pay.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.tchintech.pay.R;
import com.tchintech.pay.model.entity.BankList;
import com.tchintech.pay.widget.OnSelectTimeListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/26 0026.
 */

public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyViewHolde> {

    private Context contex;
    private List<BankList> list = new ArrayList<BankList>();

    public RecycleAdapter(Context context, List<BankList> list) {
        this.contex = context;
        this.list = list;
    }


    //设置item的布局
    @Override
    public MyViewHolde onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(contex).inflate(R.layout.recycle_bank, parent, false);
        MyViewHolde viewHolder = new MyViewHolde(view);
        return viewHolder;
    }

    //对item的数据绑定
    @Override
    public void onBindViewHolder(final MyViewHolde holder, int position) {
        final BankList banklist = list.get(position);
        holder.tv_bank.setText(banklist.getBankName());
        setSrc((ImageView) holder.iv_bank, banklist.getBankivname());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name= banklist.getBankivname();
                mOnItemClickLitener.onItemClick(holder.itemView, name);

            }
        }) ;

    }

    private void setSrc(ImageView imageView, String bankibname) {
        switch (bankibname) {
            case "bank01":
                imageView.setImageDrawable(ContextCompat.getDrawable(contex,R.drawable.bank01));
                break;
            case "bank02":
                imageView.setImageDrawable(ContextCompat.getDrawable(contex,R.drawable.bank02));
                break;
            case "bank03":
                imageView.setImageDrawable(ContextCompat.getDrawable(contex,R.drawable.bank03));
                break;
            case "bank04":
                imageView.setImageDrawable(ContextCompat.getDrawable(contex,R.drawable.bank04));
                break;
            case "bank05":
                imageView.setImageDrawable(ContextCompat.getDrawable(contex,R.drawable.bank05));
                break;
            case "bank06":
                imageView.setImageDrawable(ContextCompat.getDrawable(contex,R.drawable.bank06));
                break;
            case "bank07":
                imageView.setImageDrawable(ContextCompat.getDrawable(contex,R.drawable.bank07));
                break;
            case "bank08":
                imageView.setImageDrawable(ContextCompat.getDrawable(contex,R.drawable.bank08));
                break;
            case "bank09":
                imageView.setImageDrawable(ContextCompat.getDrawable(contex,R.drawable.bank09));
                break;
        }

    }

    //数据源的长度
    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolde extends RecyclerView.ViewHolder {
        TextView tv_bank;
        ImageView iv_bank;

        public MyViewHolde(View itemView) {
            super(itemView);
            tv_bank = (TextView) itemView.findViewById(R.id.tv_bank);
            iv_bank = (ImageView) itemView.findViewById(R.id.image_bank);

        }
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, String name);

    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
}
